package jp.co.dailyReport.weeklyReport;

import java.sql.SQLException;
import java.util.ArrayList;
import org.apache.log4j.Logger;
import java.util.Calendar;
import jp.co.dailyReport.ExecuterManager;
import jp.co.dailyReport.ConnectionFactory;
import jp.co.dailyReport.DataUnavailableException;
import jp.co.dailyReport.PermissionException;
import jp.co.dailyReport.PermissionManager;
import jp.co.dailyReport.staff.Staff;
import jp.co.dailyReport.weeklyReport.sql.WeeklyReportInsertExecuter;
import jp.co.dailyReport.weeklyReport.sql.WeeklyReportLoadMaxIDExecuter;
import jp.co.dailyReport.weeklyReport.sql.WeeklyReportLoadExecuter;
import jp.co.dailyReport.weeklyReport.sql.WeeklyReportsLoadExecuter;
import jp.co.dailyReport.weeklyReport.sql.WeeklyReportUpdateExecuter;
import jp.co.dailyReport.weeklyReport.sql.WeeklyReportDeleteExecuter;
import jp.co.dailyReport.weeklyReport.sql.WeeklyReportLoadForEditExecuter;

/**
 * 週報情報管理クラス
 */
public class WeeklyReportManager {

	private static Logger log = Logger.getLogger(WeeklyReportManager.class);

	/**
	 * 週報情報を取得します。
	 * 
	 * @return 週報オブジェクトの配列
	 * @throws DataUnavailableException
	 */
	public static WeeklyReport[] load() throws DataUnavailableException {

		ConnectionFactory connectionFactory = new ConnectionFactory();
		WeeklyReportsLoadExecuter dailyReportsLoadExecuter = new WeeklyReportsLoadExecuter();
		Object objs[] = (Object[]) null;
		try {
			objs =
				ExecuterManager.execute(
					connectionFactory,
					dailyReportsLoadExecuter);
		} catch (SQLException sqle) {
			throw new DataUnavailableException(sqle.toString());
		}
		if (objs == null || objs.length == 0) {
			return new WeeklyReport[0];
		} else {
			return (WeeklyReport[]) objs;
		}
	}

	/**
	 * 社員の権限で週報情報を取得します。
	 * 
	 * @param staff 社員オブジェクト
	 * @param methodId 操作方法ID(PermissionManagerで定義)
	 * @return 週報オブジェクトの配列
	 * @throws DataUnavailableException、PermissionException 権限が無いときに発生します。
	 */
	public static WeeklyReport[] load(Staff staff,int methodId)
		 throws DataUnavailableException {

		WeeklyReport[] weeklyReports = load();
		ArrayList answer = new ArrayList();
		for(int i =0; i < weeklyReports.length;i++){
			if(PermissionManager.hasPermission(staff,weeklyReports[i],methodId)){
				answer.add(weeklyReports[i]);
			}
		}
		return (WeeklyReport[])answer.toArray(new WeeklyReport[0]);
	}

	/**
	 * 週報情報を取得します。
	 * 
	 * @param weeklyReportId 週報ID
	 * @return 週報オブジェクト
	 * @throws DataUnavailableException
	 */
	public static WeeklyReport load(int weeklyReportId) throws DataUnavailableException {

		ConnectionFactory connectionFactory = new ConnectionFactory();
		WeeklyReportLoadExecuter weeklyReportLoadExecuter = new WeeklyReportLoadExecuter(weeklyReportId);
		Object objs[] = null;

		try {
			objs = ExecuterManager.execute(connectionFactory, weeklyReportLoadExecuter);
		} catch (SQLException sqle) {
			throw new DataUnavailableException(sqle.toString());
		}
		if (objs == null || objs.length == 0) {
			return new WeeklyReport();
		} else {
			return (WeeklyReport)objs[0];
		}
	}

	/**
	 * 週報情報を取得します。
	 * 
	 * @param projectId プロジェクトID
	 * @param workDateStart 指定作業開始日
	 * @param workDateEnd 指定作業終了日
	 * @return 週報オブジェクト
	 * @throws DataUnavailableException
	 */
	public static WeeklyReport load(int projectId,Calendar workDateStart,Calendar workDateEnd) throws DataUnavailableException {

		ConnectionFactory connectionFactory = new ConnectionFactory();
		WeeklyReportLoadForEditExecuter weeklyReportLoadForEditExecuter = new WeeklyReportLoadForEditExecuter(projectId,workDateStart,workDateEnd);
		Object objs[] = null;

		try {
			objs = ExecuterManager.execute(connectionFactory, weeklyReportLoadForEditExecuter);
		} catch (SQLException sqle) {
			throw new DataUnavailableException(sqle.toString());
		}
		if (objs == null || objs.length == 0) {
			return new WeeklyReport();
		} else {
			return (WeeklyReport)objs[0];
		}
	}

	/**
	 * 週報IDの最大値を取得します。
	 * 
	 * @return 週報IDの最大値
	 * @throws DataUnavailableException
	 */
	public static int loadMaxID() throws DataUnavailableException {

		ConnectionFactory connectionFactory = new ConnectionFactory();
		WeeklyReportLoadMaxIDExecuter weeklyReportLoadMaxIDExecuter = new WeeklyReportLoadMaxIDExecuter();
		Object objs[] = (Object[]) null;

		try {
			objs = ExecuterManager.execute(connectionFactory, weeklyReportLoadMaxIDExecuter);
		} catch (SQLException sqle) {
			throw new DataUnavailableException(sqle.toString());
		}
		if (objs == null || objs.length == 0) {

			return 0;
		} else {
			return Integer.parseInt((String) objs[0]);
		}
	}

	/**
	 * 社員の権限で週報情報を更新します。
	 * 
	 * @param staff 社員オブジェクト
	 * @param weeklyReport 週報オブジェクト
	 * @throws DataUnavailableException、PermissionException 権限が無いときに発生します。
	 */
	public static void update(Staff staff ,WeeklyReport weeklyReport) throws DataUnavailableException ,PermissionException{

		if(PermissionManager.hasPermission(staff,weeklyReport,PermissionManager.METHOD_WRITE)){
			ConnectionFactory connectionFactory = new ConnectionFactory();
			WeeklyReportUpdateExecuter weeklyReportUpdateExecuter = new WeeklyReportUpdateExecuter(weeklyReport);
			try {
				ExecuterManager.execute(connectionFactory, weeklyReportUpdateExecuter);
				WeeklyReportWorkItemManager.delete(weeklyReport.getWeeklyReportId());
			} catch (SQLException sqle) {
				log.error(sqle.toString(), sqle);
				throw new DataUnavailableException(sqle.toString());
			}
		}else{
			throw new PermissionException(" Permission denied to update weeklyReport");
		}
	}

	/**
	 * 週報情報を登録します。
	 * 
	 * @param weeklyReport 週報オブジェクト
	 * @throws DataUnavailableException
	 */
	public static void insert(WeeklyReport weeklyReport) throws DataUnavailableException {

		ConnectionFactory connectionFactory = new ConnectionFactory();
		WeeklyReportInsertExecuter weeklyReportInsertExecuter = new WeeklyReportInsertExecuter(weeklyReport);

		try {
			ExecuterManager.execute(connectionFactory, weeklyReportInsertExecuter);
		} catch(SQLException sqle) {
			log.error(sqle.toString(), sqle);
			throw new DataUnavailableException(sqle.toString());
		}
	}

	/**
	 * 社員の権限で週報情報を登録します。
	 * 
	 * @param staff 社員オブジェクト
	 * @param weeklyReport 週報オブジェクト
	 * @throws DataUnavailableException、PermissionException 権限が無いときに発生します。
	 */
	public static void insert(Staff staff ,WeeklyReport weeklyReport)
		 throws DataUnavailableException ,PermissionException{

		if(PermissionManager.hasPermission(staff,weeklyReport,PermissionManager.METHOD_WRITE)){
			insert(weeklyReport);
		}else{
			throw new PermissionException(" Permission denied to insert weeklyReport");
		}
	}

	/**
	 * 社員の権限で週報情報を削除します。
	 * 
	 * @param staff 社員オブジェクト
	 * @param weeklyReport 週報オブジェクト
	 * @throws DataUnavailableException、PermissionException 権限が無いときに発生します。
	 */
	public static void delete(Staff staff ,WeeklyReport weeklyReport) throws DataUnavailableException ,PermissionException{

		if(PermissionManager.hasPermission(staff,weeklyReport,PermissionManager.METHOD_WRITE)){
			ConnectionFactory connectionFactory = new ConnectionFactory();
			WeeklyReportDeleteExecuter dailyReportDeleteExecuter = new WeeklyReportDeleteExecuter(weeklyReport.getWeeklyReportId());
			try {
				ExecuterManager.execute(connectionFactory, dailyReportDeleteExecuter);
				WeeklyReportWorkItemManager.delete(weeklyReport.getWeeklyReportId());
			} catch (SQLException sqle) {
				log.error(sqle.toString(), sqle);
				throw new DataUnavailableException(sqle.toString());
			}
		} else {
			throw new PermissionException(" Permission denied to delete weeklyReport");
		}
	}
}
